<?php

namespace july\julyadmin\builder\traits\actions;

/**
 * 删除
 */

trait HasDelete
{
    public function delete()
    {
        $info = input('post.');
        if (!isset($info[$this->getPk()]) || !$info[$this->getPk()]) {
            $this->error('主键信息不能为空！');
        }
        $pk  = trim($info[$this->getPk()]);
        $pks = explode(",", $pk);

        foreach ($pks as $k => $v) {
            if (isset($this->noDeletePrimarykeys) && in_array($v, $this->noDeletePrimarykeys)) {
                $this->error('当前数据不允许删除！');
            }
        }

        foreach ($pks as $k => $v) {
            $find = $this->dataModel->where($this->getPk(), $v)->find();
            if (!$find) {
                $this->error('要删除的信息不存在！');
            }
            $res = $find->delete();
            if (!$res) {
                $this->error('删除失败！');
            }
        }
        $this->success('删除成功！');
    }
}
